package com.yxhl.zoume.core.expresscar.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.yxhl.protobuf.MpvModel;
import com.yxhl.protobuf.OrderBizType;
import com.yxhl.zoume.R;
import com.yxhl.zoume.common.adapter.BaseAdapter;
import com.yxhl.zoume.core.expresscar.event.RentHourlyVehicleSelectEvent;
import com.yxhl.zoume.core.expresscar.event.SingleWayVehicleSelectEvent;
import com.yxhl.zoume.data.rxbus.RxBus;
import com.yxhl.zoume.utils.DensityUtils;
import com.yxhl.zoume.utils.DeviceUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Author：zhongshan   
 * Date：2016/12/07 17:04      
 * Description：
 */
public class VehicleAdapter extends BaseAdapter<BaseAdapter.BaseViewHolder> {

    private static final int VEHICLE_COUNT = 3;
    private List<MpvModelSelectedWrapper> mVehicleModelsList = new ArrayList<>();
    private Context mContext;
    private int mSelectedPos = -1;
    private int vehicleDisplayWith;

    private OrderBizType orderBizType;

    public VehicleAdapter(OrderBizType orderBizType) {
        this.orderBizType = orderBizType;
    }

    public void setVehicleModels(List<MpvModel> list) {
        for (MpvModel mpvModel : list) {
            if (mpvModel != null)
                mVehicleModelsList.add(new MpvModelSelectedWrapper(mpvModel));
        }
        if (mVehicleModelsList.size() > 0)
            notifyDataSetChanged();
    }

    public boolean isVehicleSelected() {
        return mSelectedPos > -1;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_show_car, parent, false);
        int mVehicleTotalDisplayWith = DeviceUtils.getScreenWidth(mContext) - DensityUtils.dip2px(mContext, 40 * 2);
        vehicleDisplayWith = mVehicleTotalDisplayWith / VEHICLE_COUNT;
        return new VehicleViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(BaseAdapter.BaseViewHolder holder, int position) {
        VehicleViewHolder viewHolder = (VehicleViewHolder) holder;
        viewHolder.onBindViewHolder(position);
    }

    @Override
    public int getItemCount() {
        return mVehicleModelsList.size();
    }

    class VehicleViewHolder extends BaseAdapter.BaseViewHolder {
        @BindView(R.id.tv_vehicle_name)
        TextView mTvVehicleName;
        @BindView(R.id.iv_vehicle)
        ImageView mIvVehiclePic;
        @BindView(R.id.ll_vehicle)
        LinearLayout mLlVehicle;

        private MpvModelSelectedWrapper mpvWrapper;
        private int mPosition;

        public VehicleViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @OnClick(R.id.ll_vehicle)
        public void onClick() {

            if (!mpvWrapper.getSelected()) {
                mpvWrapper.setSelected(true);
                //加载当前选中
                loadImage(mpvWrapper.getMpvModel().getImgSelectedUrl());
                mTvVehicleName.setTextColor(mContext.getResources().getColor(R.color.font_color_orange_light));
                //设置上一个未选中
                if (mSelectedPos > -1) {
                    mVehicleModelsList.get(mSelectedPos).setSelected(false);
                    notifyItemChanged(mSelectedPos);
                }
                //更新选中位置
                mSelectedPos = mPosition;

                String modelType = mpvWrapper.getMpvModel().getModelType();
                if (OrderBizType.SPECIAL_BUS_WAY.equals(orderBizType)) {
                    RxBus.getInstance().send(new SingleWayVehicleSelectEvent(modelType));
                } else if (OrderBizType.SPECIAL_BUS_TIME.equals(orderBizType)) {
                    RxBus.getInstance().send(new RentHourlyVehicleSelectEvent(modelType));
                }
            }
        }

        public void onBindViewHolder(int position) {
            if (position >= 0 && position < mVehicleModelsList.size()) {
                setLlParams();
                mPosition = position;
                mpvWrapper = mVehicleModelsList.get(position);
                if (mpvWrapper != null) {
                    String modelName = mpvWrapper.getMpvModel().getModelName();
                    if (!TextUtils.isEmpty(modelName)) {
                        mTvVehicleName.setText(modelName);
                        mTvVehicleName.setTextColor(mContext.getResources().getColor(R.color.font_light_black));
                    }
                    loadImage(mpvWrapper.getMpvModel().getImgUrl());
                }
            }
        }

        private void setLlParams() {
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                    vehicleDisplayWith, LinearLayout.LayoutParams.WRAP_CONTENT);
            mLlVehicle.setLayoutParams(layoutParams);
        }

        private void loadImage(String url) {
            Picasso.with(mContext)
                    .load(url)
                    .placeholder(mIvVehiclePic.getDrawable())
                    .into(mIvVehiclePic);
        }
    }

    /**
     * 添加了选择标记
     */
    class MpvModelSelectedWrapper {
        private MpvModel mpvModel;
        private Boolean isSelected;

        MpvModelSelectedWrapper(MpvModel mpvModel) {
            this.mpvModel = mpvModel;
            isSelected = false;
        }

        MpvModel getMpvModel() {
            return mpvModel;
        }

        Boolean getSelected() {
            return isSelected;
        }

        MpvModelSelectedWrapper setSelected(Boolean selected) {
            isSelected = selected;
            return this;
        }
    }

}
